<template>
  <div class="home">
    <el-container>
      <el-aside width="aoto">
        <div class="logo"></div>
        <el-menu default-active="1-1" class="el-menu-admin" @open="handleOpen" @close="handleClose" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" :unique-opened='true' :collapse='iscollapse' :router="true">
          <el-submenu :index="'/'+value.path" v-for="value in navData" :key="value.id">
            <template slot="title">
              <i class="el-icon-view"></i>
              <span>{{value.authName}}</span>
            </template>
            <el-menu-item :index="'/'+value1.path" v-for="value1 in value.children" :key="value1.id" >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{value1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <!-- <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-view"></i>
              <span>权限管理</span>
            </template>
            <el-menu-item index="Right">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>角色列表</span>
              </template>
            </el-menu-item>
            <el-menu-item index="Role">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>权限列表</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-view"></i>
              <span>商品管理</span>
            </template>
            <el-menu-item index="goodslists">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>商品列表</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="4">
            <template slot="title">
              <i class="el-icon-view"></i>
              <span>订单管理</span>
            </template>
            <el-menu-item index="4-1">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>选项1</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="5">
            <template slot="title">
              <i class="el-icon-view"></i>
              <span>数据统计</span>
            </template>
            <el-menu-item index="5-1">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>选项1</span>
              </template>
            </el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <span class="myicon myicon-menu toggle-btn" @click="iscollapse=!iscollapse"></span>
          <span class="system-title">电商后台管理系统</span>
          <div>
            <span class="welcome">您好：{{username}}</span>
            <el-button type="text" @click="loginOut">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <!-- <div >欢迎来到后台管理系统</div> -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getNavData } from "../../src/api/index.js";
export default {
  data() {
    return {
      iscollapse: false,
      username: this.getUser(),
      navData:[]
    };
  },
  mounted: function() {
    this.getUser();
    this.showNav()
  },
  methods: {
    //退出功能
    loginOut() {
      this.$confirm("您确定退出当前账号嘛？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          localStorage.removeItem("mytoken");
          localStorage.removeItem("myuser");
          this.$router.push({ name: "Login" });
        })
        .catch(() => {
          console.log("操作错误请重试");
        });
    },
    //获取用户名
    getUser() {
      return localStorage.getItem("myuser");
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    //获取左侧菜单栏
    showNav(){
      getNavData().then(res=>{
        console.log(res);    
        this.navData = res.data    
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #545c64;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #545c64;
    .title {
      color: #fff;
    }
  }
  .logo {
    height: 60px;
    background: url(../assets/logo2.jpg);
    background-position-y: -30px;
    background-repeat: no-repeat;
    background-size: cover;
    background-color: #fff;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292cf;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome {
    color: white;
  }
}
</style>